using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace QQSDK
{
    public class AdsSample : SDKBaseSample
    {
        // Use this for initialization
        public Text coinText;
        public Button bannerBtn;
        public Button hideBannerBtn;

        public Button interstitialBtn;
        public Button rewardVideoBtn;
        public Button floatingAdBtn;

        public Toggle topToggleBtn;

        public Button nativeShowBtn;
        public Button hideNativeBtn;


        void Start()
        {
            float r = 0;
            float.TryParse("0.00298", out r);
            Log("ecpm===" + r);
            SetAdsButtonListener();
            //TimerSchedule.Schedule(this, 5f, () =>
            //{
            //    QQSDK.AnalyticsManager.UploadAdjustUserDataToServer("AdsSample", "OnAdjustServerCallback");
            //});



        }
        public void OnAdjustServerCallback(string args)
        {
            Log("Adjust深处转策略返回" + args);
        }

        void SetAdsButtonListener()
        {
            AdsManager.instance.onImpressionDataCallback += (args) =>
            {
                Log("用户级别的数据展示" + args);

            };

            AdsManager.instance.onRewardSecretKeyCallback += (secretkey) =>
            {
                Log("激励密钥返回" + secretkey);
            };

            AdsManager.instance.onAttributionDataCallback += (att_data) =>
            {
                Log("归因数据回传" + JsonMapper.ToJson(att_data));
            };


            AdsManager.instance.onAbnormalUserCallback += (jsonStr) =>
            {
                Log("第三方检测用户信息返回" + jsonStr);
            };

            AdsManager.instance.onAnalyticsCallback += (network) =>
            {
                Log("IOSAdjustnetwork信息:" + network);
            };

            AdsManager.instance.onBannerImpression += () =>
            {
                Log("Banner显示成功");
            };
            AdsManager.instance.onBannerClick += () =>
            {
                Log("Banner点击");
            };
            if (AdsManager.instance.IsAvailableInterstitial())
            {
                interstitialBtn.interactable = true;
            }
            if (AdsManager.instance.IsAvailableRewardVideo())
            {
                rewardVideoBtn.interactable = true;
            }
            AdsManager.instance.onGetAdvertismentID = (x) =>
            {
                Log("获取到的广告id" + x);
            };
            AdsManager.instance.onBannerLoadedFailed += () =>
            {
                Log("Banner加载失败");
            };
            AdsManager.instance.onBannerImpression += () =>
            {
                if (bannerBtn != null)
                {
                    bannerBtn.interactable = false;
                }
            // if (hideBannerBtn != null)
            // {
            //     hideBannerBtn.interactable = true;
            // }
        };
            AdsManager.instance.onInterstitialRequest += () =>
            {
                Log("插屏广告正在加载中");
            };
            AdsManager.instance.onInterstitialLoadedFailed += () =>
            {
                Log("插屏广告加载失败");
            };
            AdsManager.instance.onInterstitialOpen += () =>
            {
                Log("插屏广告打开");
                interstitialBtn.interactable = false;
            };
            AdsManager.instance.onInterstitialClose += () =>
            {
                Log("插屏广告关闭");
                BackgroundMusic(true);
            };
            AdsManager.instance.onInterstitialClick += () =>
            {
                Log("插屏广告点击");
            };

            AdsManager.instance.onInterstitialLoaded += () =>
            {
                Log("插屏广告加载成功");
                interstitialBtn.interactable = true;
            };
            AdsManager.instance.onIntersPageLoadedFailed += () =>
            {
                Log("插页式插屏广告加载失败");
            };
            AdsManager.instance.onIntersPageOpen += () =>
            {
                Log("插页式插屏广告打开");
                hideBannerBtn.interactable = false;
            };
            AdsManager.instance.onIntersPageClose += () =>
            {
                Log("插页式插屏广告关闭");
            };
            AdsManager.instance.onIntersPageClick += () =>
            {
                Log("插页式插屏广告点击");
            };

            AdsManager.instance.onIntersPageLoaded += () =>
            {
                Log("插页式插屏广告加载成功");
                hideBannerBtn.interactable = true;
            };
            AdsManager.instance.onRewardLoadFialed += () =>
            {
                Log("激励视频加载失败");
            };
            AdsManager.instance.onRewardLoaded += () =>
            {
                Log("激励加载成功");
                rewardVideoBtn.interactable = true;
            };
            AdsManager.instance.onRewardInterrupt += () =>
            {
                Log("中途关闭广告");
                BackgroundMusic(true);
            };
            AdsManager.instance.onRewardOpen += () =>
            {
                rewardVideoBtn.interactable = false;
                Log("激励打开");
            };
            AdsManager.instance.onRewardClick += () =>
            {
                Log("激励点击");
            };
            AdsManager.instance.onRewardPlayError += () =>
            {
                Log("激励视频播放失败");
            };
            AdsManager.instance.onRewardComplete += () =>
            {
                Log("激励用户");
                OnReward();
                BackgroundMusic(true);
            };
            AdsManager.instance.onUserRewarded += () =>
            {
                Log("激励用户拿到奖励");
                OnReward();
                BackgroundMusic(true);
            };
            AdsManager.instance.onNativeLoaded += () =>
            {
                nativeShowBtn.interactable = true;
                Log("Native加载成功");
            };
            AdsManager.instance.onNativeLoadedFailed += () =>
            {
                Log("Native加载失败");
            };
            AdsManager.instance.onNativeOpen += () =>
            {
                nativeShowBtn.interactable = false;
                Log("Native打开");
            };
            AdsManager.instance.onNativeClick += () =>
            {
                Log("Native点击");
            };
            AdsManager.instance.onNativeClose += () =>
            {
                Log("Native关闭");
            };
        }
        private void Log(object obj)
        {
            SDKFunc.Log("=====Unity OM:" + obj.ToString());
        }
        //显示banner广告
        public void OnClickShowBanner()
        {
            Log("展示Banner广告");
            AdsManager.instance.ShowBanner(topToggleBtn.isOn);
        }
        public void OnClickHideBanner()
        {
            Log("隐藏Banner广告");
            bannerBtn.interactable = true;
            hideBannerBtn.interactable = false;
            AdsManager.instance.HideBanenr();
        }
        /// <summary>
        /// 点击播放插屏广告
        /// </summary>
        public void OnClickShowInterstitialBtn()
        {
            Log("准备展示插屏广告");
            //AttributionManager.LogAdjustEvent("3vmkjc");
            if (AdsManager.instance.IsAvailableInterstitial())
            {
                Log("展示插屏广告");
                AdsManager.instance.ShowInterstitial("inter1");
                BackgroundMusic(false);
            }
            else
            {
                Log("插屏广告没有准备好");
            }
        }

        public void OnClickShowIntersPageBtn()
        {
            Log("准备展示Inters广告");
            if (AdsManager.instance.IsAvailableIntersPage())
            {
                Log("展示Inters广告");
                AdsManager.instance.ShowIntersPage("intersPage1");
                BackgroundMusic(false);
            }
            else
            {
                Log("Inters广告没有准备好");
            }
        }

        public void OnClickLoadNativeBtn()
        {
            Log("加载Native广告");
            AdsManager.instance.LoadNative(0.8, 0.3);
        }

        public void OnClickShowNativeBtn()
        {
            Log("准备展示Native广告");
            if (AdsManager.instance.IsAvailableNative())
            {
                Log("展示Native广告");
                hideNativeBtn.interactable = true;
                AdsManager.instance.ShowNative(0, 0.2);
                BackgroundMusic(false);
            }
            else
            {
                Log("Native广告没有准备好");
            }
        }

        public void OnClickHideNativeBtn()
        {
            Log("隐藏Native广告没有准备好");
            hideNativeBtn.interactable = false;
            AdsManager.instance.HideNative();
            BackgroundMusic(true);
            //建议隐藏之后，加载下一个
            AdsManager.instance.LoadNative(0.8, 0.3);
        }

        /// <summary>
        /// Ons the click show reward video button.点击播放激励视频
        /// </summary>
        public void OnClickShowRewardVideoBtn()
        {
            Log("准备展示激励广告");
            //AttributionManager.LogAdjustEvent("j2tr9d");
            if (AdsManager.instance.IsAvailableRewardVideo())
            {
                Log("展示激励广告");
                AdsManager.instance.ShowRewardVideo("rv1");
                BackgroundMusic(false);
            }
            else
            {
                Log("激励广告没有准备好");
            }

        }

        /// <summary>
        /// 广告开关的时候，要处理背景音乐的停止和播放逻辑
        /// onInterstitialClose
        /// onRewardComplete
        /// onRewardInterrupt  这三个开启
        ///      ShowRewardAd
        ///      ShowInterisitalAd 这两个关闭
        /// </summary>
        /// <param name="isMu"></param>
        private void BackgroundMusic(bool isMu)
        {
            Log("游戏背景音乐状态："+ isMu);
            //TODO:处理背景音乐逻辑
        }

        int coin;
        void OnReward()
        {
            if (coinText != null)
            {
                coin++;
                coinText.text = "金币数量:" + coin.ToString();
            }
        }
        private void OnDestroy()
        {
        }
    }
}